<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if(echarts){
      // console.log(echarts);
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      }
    },
    setOptions({ actualData,actualData1,actualData2,actualData3,actualData4 ,actualData5,expectedData} = {}) {
      this.chart.setOption({
        xAxis: {
          data: expectedData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [  {
          name:"Price",      
          axisLine:{
          lineStyle:{
            color:'#737F89'
          }
        },    
      splitLine: {
            show: false // 隐藏 y 轴的分割线
        },                 
      type: 'value'
    }],
        xAxis: [
          {
            type: 'category',
            data:expectedData,
            axisLine:{
              lineStyle:{
                color:'#737F89'
              }
            }
          }
        ],
        legend: {
          show:false,
          bottom:"bottom",
          left: 'center'
        },
        series: [
          {
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#34C717',
                lineStyle: {
                  color: '#34C717',
                  width: 4
                },              
              }
            },
            
            data: actualData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },

        ]
      })
    }
  }
}
</script>
